﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace PatsTracks
{
    [Serializable]
    public class MediaDB
    {
        List<KeyValuePair<int, Media>> mediaList;
        public MediaDB()
        {
            mediaList = new List<KeyValuePair<int, Media>>();
            mediaList = mediaList.OrderBy(x => x.Value.Title).ToList();
        }
        public void AddOrUpdateMedia(ref Media med)
        {
            foreach (var i in mediaList)
                if (i.Key == med.getSerialNumber)
                {
                    mediaList.Remove(i);
                    mediaList.Add(new KeyValuePair<int, Media>(med.getSerialNumber, med));
                    return;
                }
            mediaList.Add(new KeyValuePair<int, Media>(med.getSerialNumber, med));
        }
        public int getHighestKey()
        {
            int temp = 0;
            foreach (var i in mediaList)
            {
                if (i.Key > temp) temp = i.Key;
            }
            return temp;
        }
        public List<Media> getAllMedia()
        {
            List<Media> retval = new List<Media>();
            foreach (var i in mediaList)
                retval.Add(i.Value);
            retval = retval.OrderBy(x => x.Title).ToList();
            return retval;
        }
        public Media Find(int serialnum)
        {
            foreach (var i in mediaList)
                if (serialnum == i.Key)
                    return i.Value;
            throw new Exception("Media did not exist");
        }
        public void Remove(int serialnum)
        {
            foreach (var i in mediaList)
                if (serialnum == i.Key)
                {
                    mediaList.Remove(i);
                    return;
                }
            throw new Exception("Media did not exist");
        }
        public void Save()
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream("Media.dat", FileMode.OpenOrCreate, FileAccess.Write);
            bf.Serialize(fs, mediaList);
            fs.Close();
        }
        public void Load()
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream("Media.dat", FileMode.Open, FileAccess.Read);
            mediaList = (List<KeyValuePair<int, Media>>)bf.Deserialize(fs);
            fs.Close();
        }
    }
    [Serializable]
    public class Media
    {
        public string Title { get; set; }
        public string Artist { get; set; }
        public string Genre { get; set; }
        public string Decade { get; set; }
        int SerialNumber;
        public double PurchasePrice { get; set; }
        public bool sold { get; set; }
        public double SalePrice { get; set; }

        public int getSerialNumber
        {
            get
            {
                return SerialNumber;
            }
        }
        public override string ToString()
        {
            return Title + ", " + Artist + ", SN: " + SerialNumber.ToString();
            //return "Title: " + Title + "  Artist: " + Artist + "\t\t\t\t\t\t\t\t\t SN: " + SerialNumber.ToString();
        }

        public Media(string Title, string Artist, string Genre, string Decade, int serialnumber, double purchaseprice, bool sold, double saleprice)
        {
            this.Title = Title;
            this.Artist = Artist;
            this.Genre = Genre;
            this.Decade = Decade;
            this.SerialNumber = serialnumber;
            this.PurchasePrice = purchaseprice;
            this.SalePrice = saleprice;
        }
    }
}
